home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / gs262 / zupath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-29  |  11.9 KB  |  502 lines

  1. /* Copyright (C) 1990, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zupath.c */
  20. /* Operators related to user paths */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "dict.h"
  25. #include "dstack.h"
  26. #include "iutil.h"
  27. #include "state.h"
  28. #include "store.h"
  29. #include "stream.h"
  30. #include "bnum.h"
  31. #include "gsmatrix.h"
  32. #include "gspath.h"
  33. #include "gsstate.h"
  34. #include "gscoord.h"
  35. #include "gspaint.h"
  36. #include "gxfixed.h"
  37. #include "gxdevice.h"
  38. #include "gxpath.h"
  39.  
  40. /* Forward references */
  41. private int upath_append(P1(os_ptr));
  42. private int upath_stroke(P1(os_ptr));
  43.  
  44. #ifdef AMIGA
  45. /* External references */
  46. extern void gx_set_device_only(gs_state *, gx_device *);
  47. #endif
  48.  
  49. /* ------ Insideness testing ------ */
  50.  
  51. /* Forward references */
  52. private int in_test(P2(os_ptr, int (*)(P1(gs_state *))));
  53. private int in_path(P1(os_ptr));
  54. private int in_path_result(P3(os_ptr, int, int));
  55. private int in_utest(P2(os_ptr, int (*)(P1(gs_state *))));
  56. private int in_upath(P1(os_ptr));
  57. private int in_upath_result(P3(os_ptr, int, int));
  58.  
  59. /* We use invalidexit, which the painting procedures cannot generate, */
  60. /* as an "error" to indicate that the hit detection device found a hit. */
  61. #define e_hit e_invalidexit
  62.  
  63. /* <x> <y> ineofill <bool> */
  64. /* <userpath> ineofill <bool> */
  65. int
  66. zineofill(os_ptr op)
  67. {    return in_test(op, gs_eofill);
  68. }
  69.  
  70. /* <x> <y> infill <bool> */
  71. /* <userpath> infill <bool> */
  72. int
  73. zinfill(os_ptr op)
  74. {    return in_test(op, gs_fill);
  75. }
  76.  
  77. /* <x> <y> instroke <bool> */
  78. /* <userpath> instroke <bool> */
  79. int
  80. zinstroke(os_ptr op)
  81. {    return in_test(op, gs_stroke);
  82. }
  83.  
  84. /* <x> <y> <userpath> inueofill <bool> */
  85. /* <userpath1> <userpath2> inueofill <bool> */
  86. int
  87. zinueofill(os_ptr op)
  88. {    return in_utest(op, gs_eofill);
  89. }
  90.  
  91. /* <x> <y> <userpath> inufill <bool> */
  92. /* <userpath1> <userpath2> inufill <bool> */
  93. int
  94. zinufill(os_ptr op)
  95. {    return in_utest(op, gs_fill);
  96. }
  97.  
  98. /* <x> <y> <userpath> inustroke <bool> */
  99. /* <userpath1> <userpath2> inustroke <bool> */
  100. int
  101. zinustroke(os_ptr op)
  102. {    /* This is different because of the optional matrix operand. */
  103.     int code = gs_gsave(igs);
  104.     int spop, npop;
  105.     if ( code < 0 ) return code;
  106.     if ( (spop = upath_stroke(op)) < 0 ||
  107.          (npop = in_path(op - spop)) < 0
  108.        )
  109.        {    gs_grestore(igs);
  110.         return code;
  111.        }
  112.     code = gs_stroke(igs);
  113.     return in_upath_result(op, npop + spop, code);
  114. }
  115.  
  116. /* ------ Internal routines ------ */
  117.  
  118. /* Define a minimal device for insideness testing. */
  119. /* It returns e_hit whenever it is asked to actually paint any pixels. */
  120. private dev_proc_fill_rectangle(hit_fill_rectangle);
  121. private gx_device_procs hit_procs = {
  122.     NULL,                /* open_device */
  123.     NULL,                /* get_initial_matrix */
  124.     NULL,                /* sync_output */
  125.     NULL,                /* output_page */
  126.     NULL,                /* close_device */
  127.     gx_default_map_rgb_color,
  128.     gx_default_map_color_rgb,
  129.     hit_fill_rectangle,
  130.     NULL,                /* tile_rectangle */
  131.     NULL,                /* copy_mono */
  132.     NULL,                /* copy_color */
  133.     gx_default_draw_line
  134. };
  135. private gx_device hit_device =
  136. {    sizeof(gx_device),
  137.     &hit_procs,
  138.     "hit detector",
  139.     0, 0, 1, 1, no_margins, dci_black_and_white, 0    /* generic */
  140. };
  141. /* Test for a hit when filling a rectangle. */
  142. private int
  143. hit_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  144.   gx_color_index color)
  145. {    return (w > 0 && h > 0 ? e_hit : 0);
  146. }
  147.  
  148. /* Do the work of the non-user-path insideness operators. */
  149. private int
  150. in_test(os_ptr op, int (*paintproc)(P1(gs_state *)))
  151. {    int npop = in_path(op);
  152.     int code;
  153.     if ( npop < 0 ) return npop;
  154.     code = (*paintproc)(igs);
  155.     return in_path_result(op, npop, code);
  156. }
  157.  
  158. /* Set up a clipping path and device for insideness testing. */
  159. private int
  160. in_path(os_ptr op)
  161. {    int code = gs_gsave(igs);
  162.     int npop;
  163.     float uxy[2];
  164.     if ( code < 0 ) return code;
  165.     code = num_params(op, 2, uxy);
  166.     if ( code >= 0 )
  167.        {    /* Aperture is a single pixel. */
  168.         gs_point dxy;
  169.         gs_fixed_rect fr;
  170.         gs_transform(igs, uxy[0], uxy[1], &dxy);
  171.         fr.p.x = fixed_floor(float2fixed(dxy.x));
  172.         fr.p.y = fixed_floor(float2fixed(dxy.y));
  173.         fr.q.x = fr.p.x + int2fixed(1);
  174.         fr.q.y = fr.p.y + int2fixed(1);
  175.         code = gx_clip_to_rectangle(igs, &fr);
  176.         npop = 2;
  177.        }
  178.     else
  179.        {    /* Aperture is a user path. */
  180.         if ( (code = upath_append(op)) >= 0 )
  181.             code = gx_clip_to_path(igs);
  182.         npop = 1;
  183.        }
  184.     if ( code < 0 )
  185.        {    gs_grestore(igs);
  186.         return code;
  187.        }
  188.     /* Install the hit detection device. */
  189.     gs_setgray(igs, 0.0);
  190.     gx_set_device_only(igs, &hit_device);
  191.     return npop;
  192. }
  193.  
  194. /* Finish an insideness test. */
  195. private int
  196. in_path_result(os_ptr op, int npop, int code)
  197. {    int result;
  198.     gs_grestore(igs);
  199.     switch ( code )
  200.        {
  201.     case e_hit:            /* found a hit */
  202.         result = 1;
  203.         break;
  204.     case 0:                /* completed painting without a hit */
  205.         result = 0;
  206.         break;
  207.     default:            /* error */
  208.         return code;
  209.        }
  210.     npop--;
  211.     pop(npop); op -= npop;
  212.     make_bool(op, result);
  213.     return 0;
  214.         
  215. }
  216.  
  217. /* Do the work of the user-path insideness operators. */
  218. private int
  219. in_utest(os_ptr op, int (*paintproc)(P1(gs_state *)))
  220. {    int npop = in_upath(op);
  221.     int code;
  222.     if ( npop < 0 ) return npop;
  223.     code = (*paintproc)(igs);
  224.     return in_upath_result(op, npop, code);
  225. }
  226.  
  227. /* Set up a clipping path and device for insideness testing */
  228. /* with a user path. */
  229. private int
  230. in_upath(os_ptr op)
  231. {    int code = gs_gsave(igs);
  232.     int npop;
  233.     if ( code < 0 ) return code;
  234.     if ( (code = upath_append(op)) < 0 ||
  235.          (npop = in_path(op - 1)) < 0
  236.        )
  237.        {    gs_grestore(igs);
  238.         return code;
  239.        }
  240.     return npop + 1;
  241. }
  242.  
  243. /* Finish an insideness test with a user path. */
  244. private int
  245. in_upath_result(os_ptr op, int npop, int code)
  246. {    gs_grestore(igs);        /* undo the extra gsave */
  247.     return in_path_result(op, npop, code);
  248. }
  249.  
  250. /* ------ User paths ------ */
  251.  
  252. /* User path operator codes */
  253. typedef enum {
  254.   upath_setbbox = 0,
  255.   upath_moveto = 1,
  256.   upath_rmoveto = 2,
  257.   upath_lineto = 3,
  258.   upath_rlineto = 4,
  259.   upath_curveto = 5,
  260.   upath_rcurveto = 6,
  261.   upath_arc = 7,
  262.   upath_arcn = 8,
  263.   upath_arct = 9,
  264.   upath_closepath = 10,
  265.   upath_ucache = 11
  266. } upath_op;
  267. #define upath_op_max 11
  268. #define upath_repeat 32
  269. static byte up_nargs[upath_op_max + 1] =
  270.    { 4, 2, 2, 2, 2, 6, 6, 5, 5, 5, 0, 0 };
  271. #define zp(proc) extern int proc(P1(os_ptr))
  272.     zp(zsetbbox); zp(zmoveto); zp(zrmoveto);
  273.     zp(zlineto); zp(zrlineto); zp(zcurveto); zp(zrcurveto);
  274.     zp(zarc); zp(zarcn); zp(zarct); zp(zclosepath); zp(zucache);
  275. #undef zp
  276. static op_proc_p up_ops[upath_op_max + 1] =
  277.    {    zsetbbox, zmoveto, zrmoveto, zlineto, zrlineto,
  278.     zcurveto, zrcurveto, zarc, zarcn, zarct,
  279.     zclosepath, zucache
  280.    };
  281.  
  282. /* - ucache - */
  283. int
  284. zucache(os_ptr op)
  285. {    /* A no-op for now. */
  286.     return 0;
  287. }
  288.  
  289. /* <userpath> uappend - */
  290. int
  291. zuappend(register os_ptr op)
  292. {    int code = gs_gsave(igs);
  293.     if ( code < 0 ) return code;
  294.     if ( (code = upath_append(op)) >= 0 )
  295.         code = gs_upmergepath(igs);
  296.     gs_grestore(igs);
  297.     if ( code < 0 ) return code;
  298.     pop(1);
  299.     return 0;
  300. }
  301.  
  302. /* <userpath> ueofill - */
  303. int
  304. zueofill(register os_ptr op)
  305. {    int code = gs_gsave(igs);
  306.     if ( code < 0 ) return code;
  307.     if ( (code = upath_append(op)) >= 0 )
  308.         code = gs_eofill(igs);
  309.     gs_grestore(igs);
  310.     if ( code < 0 ) return code;
  311.     pop(1);
  312.     return 0;
  313. }
  314.  
  315. /* <userpath> ufill - */
  316. int
  317. zufill(register os_ptr op)
  318. {    int code = gs_gsave(igs);
  319.     if ( code < 0 ) return code;
  320.     if ( (code = upath_append(op)) >= 0 )
  321.         code = gs_fill(igs);
  322.     gs_grestore(igs);
  323.     if ( code < 0 ) return code;
  324.     pop(1);
  325.     return 0;
  326. }
  327.  
  328. /* <userpath> ustroke - */
  329. /* <userpath> <matrix> ustroke - */
  330. int
  331. zustroke(register os_ptr op)
  332. {    int code = gs_gsave(igs);
  333.     int npop;
  334.     if ( code < 0 ) return code;
  335.     if ( (code = npop = upath_stroke(op)) >= 0 )
  336.         code = gs_stroke(igs);
  337.     gs_grestore(igs);
  338.     if ( code < 0 ) return code;
  339.     pop(npop);
  340.     return 0;
  341. }
  342.  
  343. /* <userpath> ustrokepath - */
  344. /* <userpath> <matrix> ustrokepath - */
  345. int
  346. zustrokepath(register os_ptr op)
  347. {    int code = gs_gsave(igs);
  348.     int npop;
  349.     if ( code < 0 ) return code;
  350.     if ( (code = npop = upath_stroke(op)) < 0 ||
  351.          (code = gs_strokepath(igs)) < 0 ||
  352.          (code = gs_upmergepath(igs)) < 0
  353.        )
  354.         ;
  355.     gs_grestore(igs);
  356.     if ( code < 0 ) return code;
  357.     pop(npop);
  358.     return 0;
  359. }
  360.  
  361. /* --- Internal routines --- */
  362.  
  363. /* Append a user path to the current path. */
  364. private int
  365. upath_append(os_ptr op)
  366. {    check_read(*op);
  367.     gs_newpath(igs);
  368.     /****** ROUND tx AND ty ******/
  369.     if ( r_has_type(op, t_array) && r_size(op) == 2 &&
  370.          r_has_type(op->value.refs + 1, t_string)
  371.        )
  372.        {    /* 1st element is operators, 2nd is operands */
  373.         stream st;
  374.         int code;
  375.         int repcount = 1;
  376.         const byte *opp;
  377.         uint ocount;
  378.         code = sread_num_array(&st, op->value.refs);
  379.         if ( code < 0 ) return code;
  380.         opp = op->value.refs[1].value.bytes;
  381.         ocount = r_size(&op->value.refs[1]);
  382.         while ( ocount-- )
  383.            {    byte opx = *opp++;
  384.             if ( opx > 32 )
  385.                 repcount = opx - 32;
  386.             else if ( opx > upath_op_max )
  387.                 return_error(e_typecheck);
  388.             else        /* operator */
  389.                {    do
  390.                    {    byte opargs = up_nargs[opx];
  391.                     while ( opargs-- )
  392.                        {    push(1);
  393.                         code = sget_encoded_number(&st, op);
  394.                         switch ( code )
  395.                            {
  396.                         case t_integer:
  397.                             r_set_type_attrs(op, t_integer, 0);
  398.                             break;
  399.                         case t_real:
  400.                             r_set_type_attrs(op, t_real, 0);
  401.                             break;
  402.                         default:
  403.                             return_error(e_typecheck);
  404.                            }
  405.                        }
  406.                     code = (*up_ops[opx])(op);
  407.                     if ( code < 0 ) return code;
  408.                     op = osp;    /* resync */
  409.                    }
  410.                 while ( --repcount );
  411.                 repcount = 1;
  412.                }
  413.            }
  414.        }
  415.     else
  416.        {    /* Ordinary executable array */
  417.         const ref *arp = op;
  418.         ref *defp;
  419.         ref rup;
  420.         uint ocount = r_size(op);
  421.         long index = 0;
  422.         int argcount = 0;
  423.         int (*oproc)(P1(os_ptr));
  424.         int opx, code;
  425.         for ( ; index < ocount; index++ )
  426.          switch ( array_get(arp, index, &rup), r_type(&rup) )
  427.            {
  428.         case t_integer:
  429.         case t_real:
  430.             argcount++;
  431.             push(1);
  432.             *op = rup;
  433.             break;
  434.         case t_name:
  435.             if ( !r_has_attr(&rup, a_executable) )
  436.                 return_error(e_typecheck);
  437.             if ( dict_find(systemdict, &rup, &defp) <= 0 )
  438.                 return_error(e_undefined);
  439.             if ( r_btype(defp) != t_operator )
  440.                 return_error(e_typecheck);
  441.             goto xop;
  442.         case t_operator:
  443.             defp = &rup;
  444. xop:            if ( !r_has_attr(defp, a_executable) )
  445.                 return_error(e_typecheck);
  446.             oproc = real_opproc(defp);
  447.             for ( opx = 0; opx <= upath_op_max; opx++ )
  448.                 if ( oproc == up_ops[opx] ) break;
  449.             if ( opx > upath_op_max || argcount != up_nargs[opx] )
  450.                 return_error(e_typecheck);
  451.             code = (*oproc)(op);
  452.             if ( code < 0 ) return code;
  453.             op = osp;    /* resync ostack pointer */
  454.             argcount = 0;
  455.             break;
  456.         default:
  457.             return_error(e_typecheck);
  458.            }
  459.         if ( argcount )
  460.             return_error(e_typecheck);    /* leftover args */
  461.        }
  462.     return 0;
  463. }
  464.  
  465. /* Append a user path to the current path, and then apply */
  466. /* a transformation if one is supplied. */
  467. private int
  468. upath_stroke(register os_ptr op)
  469. {    int code, npop;
  470.     gs_matrix mat;
  471.     if ( (code = read_matrix(op, &mat)) >= 0 )
  472.        {    if ( (code = upath_append(op - 1)) >= 0 )
  473.             code = gs_concat(igs, &mat);
  474.         npop = 2;
  475.        }
  476.     else
  477.        {    code = upath_append(op);
  478.         npop = 1;
  479.        }
  480.     return (code < 0 ? code : npop);
  481. }
  482.  
  483. /* ------ Initialization procedure ------ */
  484.  
  485. op_def zupath_op_defs[] = {
  486.         /* Insideness testing */
  487.     {"1ineofill", zineofill},
  488.     {"1infill", zinfill},
  489.     {"1instroke", zinstroke},
  490.     {"2inueofill", zinueofill},
  491.     {"2inufill", zinufill},
  492.     {"2inustroke", zinustroke},
  493.         /* User paths */
  494.     {"1uappend", zuappend},
  495.     {"1ueofill", zueofill},
  496.     {"1ufill", zufill},
  497.     {"1ustroke", zustroke},
  498.     {"1ustrokepath", zustrokepath},
  499.     {"0ucache", zucache},
  500.     op_def_end(0)
  501. };
  502.